﻿using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 阵营权限类型
/// </summary>
public enum CampPowerType
{
   /// <summary>
   /// 攻击
   /// </summary>
   Attack = 0,
   /// <summary>
   /// 治疗
   /// </summary>
   Cure = 1, 
   /// <summary>
   /// 可跨越
   /// </summary>
   Span = 2,
}

/// <summary>
/// 阵营权限信息
/// </summary>
public struct CampPowerInfo
{
   public string Name;
   public CampPowerType PowerType;
   public string[] Camps;
   
   public CampPowerInfo(string name,CampPowerType powerType,string[] canCamps)
   {
      PowerType = powerType;
      Name = name;
      Camps = canCamps;
   } 
}

/// <summary>
/// 策划配置表时用的结构
/// </summary>
public struct CampInfoModel
{
   /// <summary>
   /// 阵营id
   /// </summary>
   public string Id;
   /// <summary>
   /// 阵营名 
   /// </summary>
   public string Name;
   /// <summary>
   /// 阵营描述 
   /// </summary>
   public string CampDes;
   /// <summary>
   /// 操作员 0 电脑 1 玩家
   /// </summary>
   public int CampController;
   /// <summary>
   /// 阵营的权力信息 对其他阵营的权力
   /// </summary>
   public CampPowerInfo[] CampPowerInfo;
   /// <summary>
   /// 回合优先级
   /// </summary>
   public int Priority;
   /// <summary>
   /// 阵营颜色
   /// </summary>
   public Color Color;

   public CampInfoModel(string id, string name, string campDes, int campController, CampPowerInfo[] campPowerInfo, int priority, Color color)
   {
      Id = id;
      Name = name;
      CampDes = campDes;
      CampController = campController;
      CampPowerInfo = campPowerInfo;
      Priority = priority;
      Color = color;
   }
}

/// <summary>
/// 阵营对象信息
/// </summary>
public class CampInfoObj
{
   /// <summary>
   /// 可操作阵营
   /// </summary>
   public Dictionary<string,CampInfoModel> Dict;
   /// <summary>
   /// 基础阵营信息
   /// </summary>
   public CampInfoModel[] Models;
   /// <summary>
   /// 初始行动阵营
   /// </summary>
   public string DefineActionCamp;

   public CampInfoObj(CampInfoModel[] canOperatorComps)
   {
      Dict = new Dictionary<string, CampInfoModel>();
      foreach (var v in canOperatorComps)
      {
         if (!Dict.ContainsKey(v.Id))
         {
           Dict.Add(v.Id,v);
         } 
      }

      Models = canOperatorComps;
      DefineActionCamp = canOperatorComps[0].Id;
   }

   /// <summary>
   /// 获取下个行动的阵营
   /// </summary>
   /// <param name="curActionCamp"></param>
   /// <returns></returns>
   public string GetNextCamp(string curActionCamp)
   {
      foreach (var v in Models)
      {
         if (v.Priority > Dict[curActionCamp].Priority)
         {
            return v.Id;
         } 
      }

      return DefineActionCamp;
   }

/// <summary>
/// 检查当前阵营对别的阵营是否有某种权限
/// </summary>
/// <param name="CampA"></param>
/// <param name="CampB"></param>
/// <param name="campPowerType"></param>
/// <returns></returns>
   public static bool CheckCompsPower(string CampA,string CampB,CampPowerType campPowerType)
   {
      if (DesignerTables.CompInfo.data[CampA].Equals(null) || DesignerTables.CompInfo.data[CampB].Equals(null))
      {
         return false;
      }
      foreach (var compInfo in DesignerTables.CompInfo.data[CampA].CampPowerInfo)
      {
         if (compInfo.PowerType.Equals(campPowerType))
         {
            for (int i = 0; i < compInfo.Camps.Length; i++)
            {
               if (compInfo.Camps[i].Equals(CampB))
               {
                  return true;
               } 
            }
         } 
      }
      return false;
   }
   
   /// <summary>
   /// 检查这个阵营是否是ai控制
   /// </summary>
   /// <param name="comp"></param>
   /// <returns></returns>
   public bool isAIControl(string campId)
   {
      return Dict[campId].CampController == 0;
   }
}